/*
Copyright 2010  Murali Devi (profdevi@gmail.com)

This file is part of ComineGL Project.

ComineGL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ComineGL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ComineGL.  If not, see <http://www.gnu.org/licenses/>.

This code uses the irrlicht and irrklang libraries.  Please read
the libraries copyrights at 

	irrlicht library:  http://irrlicht.sourceforge.net
	irrklang library:  http://www.ambiera.com/irrklang

*/



//v0.14 copyright Comine.com 20101212U01
#include "MStdLib.h"
#include "Global.h"
#include "ComineGLTask.h"


//************************************************
//* Module Elements
//************************************************
struct GTaskNoArguments
	{
	bool (*UserCallBack)(void);
	};


////////////////////////////////////////////////
struct GTaskArgInt
	{
	bool (*UserCallBack)(int arg1);
	int Arg1;
	};


////////////////////////////////////////////////
static bool GTaskNoArgumentsAction(MTimeEventQueue &manager,int eventid,void *extra)
	{	
	GTaskNoArguments *object=(GTaskNoArguments *)extra;
	if(object==NULL)
		{
		//=We got a severe error
		manager.EventCancel(eventid);
		MStdPrintf("**Task Event Failure",__LINE__);
		return false;
		}

	// Call the user function
	bool ret;
	ret=object->UserCallBack();
	if(ret==false)
		{
		//=User had requested delete of event
		manager.EventCancel(eventid);
		return true;
		}

	return true;
	}


////////////////////////////////////////////////
static bool GTaskArgsIntAction(MTimeEventQueue &manager,int eventid,void *extra)
	{	
	GTaskArgInt *object=(GTaskArgInt *)extra;
	if(object==NULL)
		{
		//=We got a severe error
		manager.EventCancel(eventid);
		MStdPrintf("**Task Event Failure",__LINE__);
		return false;
		}

	// Call the user function
	bool ret;
	ret=object->UserCallBack(object->Arg1);
	if(ret==false)
		{
		//=User had requested delete of event
		manager.EventCancel(eventid);
		return true;
		}

	return true;
	}



///////////////////////////////////////////////////
static bool GTaskOnEnd(MTimeEventQueue &manager,int eventid,void *extra)
	{
	if(extra!=NULL)
		{
		delete extra;
		}

	return true;
	}


//************************************************
//* Task Functions
//************************************************
MDLLFUN bool CGLTaskLoadNoArguments(int taskid,int deltatime,bool repeat,bool action(void))
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	if(action==NULL || deltatime<=0)
		{
		MStdPrintf("**Bad paramaters for TaskLoad",__LINE__);
		return false;
		}
	
	GTaskNoArguments *extradata=new GTaskNoArguments;
	if(extradata==NULL)
		{
		return false;
		}

	extradata->UserCallBack=action;

	bool ret;
	ret=GlobalTaskQueue.EventAdd(taskid,repeat,deltatime
				,GTaskNoArgumentsAction,GTaskOnEnd,extradata);
	if(ret==false)
		{
		return false;
		}
	
	return true;	
	}


//////////////////////////////////////////////////////////////
MDLLFUN bool CGLTaskLoadArgsInt(int taskid,int deltatime,bool repeat,bool action(int arg),int arg)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	if(action==NULL || deltatime<=0)
		{
		MStdPrintf("**Bad paramaters for TaskLoad",__LINE__);
		return false;
		}
	
	GTaskArgInt *extradata=new GTaskArgInt;
	if(extradata==NULL)
		{
		return false;
		}

	extradata->UserCallBack=action;
	extradata->Arg1=arg;

	bool ret;
	ret=GlobalTaskQueue.EventAdd(taskid,repeat,deltatime
				,GTaskArgsIntAction,GTaskOnEnd,extradata);
	if(ret==false)
		{
		return false;
		}
	
	return true;	
	}


//////////////////////////////////////////////////////
MDLLFUN bool GGLTaskUnload(int taskid)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	if(GlobalTaskQueue.EventCancel(taskid)==false)
		{
		//=Just return false for event cancel
		return false;
		}

	return true;
	}


///////////////////////////////////////////////
MDLLFUN bool CGLTaskUnloadAll(void)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	if(GlobalTaskQueue.EventCancelAll()==false)
		{
		//=Just return false for event cancel
		return false;
		}

	return true;	
	}


///////////////////////////////////////////////
MDLLFUN int CGLTaskGetCount(void)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	return GlobalTaskQueue.GetEventCount();
	}

